from collections import deque
'''
面试题 03.06. 动物收容所
'''
class pet:
    def __init__(self, type: str):
        self.__type = type
    def get_pet_type(self)->str:
        return self.__type

class dog(pet):
    def __init__(self):
        super(dog, self).__init__('dog')
class cat(pet):
    def __init__(self):
        super(cat, self).__init__('cat')
class pet_enter_queue:
    def __init__(self, ipet: pet, count: int) -> None:
        self.__ipet = ipet
        self.__count = count
    def get_pet(self)->pet:
        return self.__ipet
    def get_count(self)->int:
        return self.__count
    def get_enter_queue_type(self):
        return self.__ipet.get_pet_type()
class dog_cat_queue:
    def __init__(self):
        self.__dog_q = deque()
        self.__cat_q = deque()
        self.__count = 0
    def add(self, ipet: pet):
        if ipet.get_pet_type() == 'dog':
            self.__dog_q.append(pet_enter_queue(ipet, self.__count))
            self.__count += 1
        elif ipet.get_pet_type() == 'cat':
            self.__cat_q.append(pet_enter_queue(ipet, self.__count)) #模拟队列入队，相当于appendright
            self.__count += 1            
        else:
            raise Exception("err, not dog or cat")
    def poll_all(self)->pet:
        if len(self.__dog_q) > 0 and len(self.__cat_q):
            tmp_dog = self.__dog_q.popleft()  #模拟队列出队
            tmp_cat = self.__cat_q.popleft()
            if tmp_dog.get_count() < tmp_cat.get_count() :
                self.__cat_q.appendleft(tmp_cat)
                return tmp_dog.get_pet()
            else :
                self.__dog_q.appendleft(tmp_dog)
                return tmp_cat.get_pet()
        elif len(self.__dog_q) > 0:
            return self.__dog_q.popleft().get_pet()
        elif len(self.__cat_q) > 0:
            return self.__cat_q.popleft().get_pet()
        else:
            raise Exception("err, queue is empty!")
    def poll_dog(self)->dog:
        if not self.is_dog_queue_empty():
            return self.__dog_q.popleft().get_pet()
        else: raise Exception('Dog queue is empty!')
    def poll_cat(self)->cat:
        if not self.is_cat_queue_empty():
            return self.__cat_q.popleft().get_pet()
        else:
            raise Exception('Cat queue is empty!')        
    def is_empty(self) ->bool:
        return len(self.__dog_q) == 0 and len(self.__cat_q) == 0
    def is_dog_queue_empty(self) -> bool:
        return len(self.__dog_q) == 0
    def is_cat_queue_empty(self)->bool:
        return len(self.__cat_q) == 0
def main():
    test = dog_cat_queue()
    dog1 = dog()
    cat1 = cat()
    dog2 = dog()
    cat2 = cat()
    dog3 = dog()
    cat3 = cat()
    test.add(dog1)

    test.add(cat1)

    test.add(dog2)
    test.add(cat2)
    test.add(dog3)
    test.add(cat3)

    test.add(dog1)
    test.add(cat1)
    test.add(dog2)
    test.add(cat2)
    test.add(dog3)
    test.add(cat3)

    test.add(dog1)
    test.add(cat1)
    test.add(dog2)
    test.add(cat2)
    test.add(dog3)
    test.add(cat3)

    while not test.is_dog_queue_empty():
        print(test.poll_dog().get_pet_type())

    while not test.is_empty():
        print(test.poll_all().get_pet_type())

if __name__ == "__main__":
    main()